<?php
//模板类
class lib_template{
	//模板文件
	protected $template = 'template/template';
	//标题
	public $title = '首页';
	//面包屑导航
	public $navigate;
	//seo设置
	public $meta_keywords;
	public $meta_description;
	//css直显
	private $style = '';
	//js直显
	private $script = '';
	//脚部js直显
	private $endscript = '';
	//CSS样式表
	private $css = array();
	//脚本文件
	private $js = array();
	//脚部脚本文件
	private $endjs = array();
	//头部模板
	protected $head = '';
	protected $head_render = true;
	//底部模板
	protected $foot = '';
	protected $foot_render = true;
	//主内容
	protected $main = '';
	protected $main_render = true;
	//主内容中心
	protected $content = '';
	protected $content_render = true;
	//主内容左侧
	protected $content_left = '';
	protected $content_left_render = true;
	//主内容右侧
	protected $content_right = '';
	protected $content_right_render = true;
	//附加内容
	protected $extra = '';
	protected $extra_render = true;
	//网站小图标
	protected $ico = 'favicon.ico';
	
	function __construct($template = null){
		if (is_string($template)) $this->template = $template;
		$this->meta_keywords = project::config()->meta_keywords;
		$this->meta_description = project::config()->meta_description;
	}
	
	public function head($view = null){
		if (($view instanceof View) || is_string($view)){
			$this->head = $view;
			return $this;
		}
		return false;
	}
	public function foot($view){
		if (($view instanceof View) || is_string($view)){
			$this->foot = $view;
			return $this;
		}
		return false;
	}
	public function main($view){
		if (($view instanceof View) || is_string($view)){
			$this->main = $view;
			return $this;
		}
		return false;
	}
	public function content($view){
		if ((($view instanceof View) || is_string($view)) || is_string($view)){
			$this->content = $view;
			return $this;
		}
		return false;
	}
	public function content_left($view){
		if (($view instanceof View) || is_string($view)){
			$this->content_left = $view;
			return $this;
		}
		return false;
	}
	public function content_right($view){
		if (($view instanceof View) || is_string($view)){
			$this->content_right = $view;
			return $this;
		}
		return false;
	}
	public function extra($view){
		if (($view instanceof View) || is_string($view)){
			$this->extra = $view;
			return $this;
		}
		return false;
	}
	
	//获取视图部件对象
	public function getObject($object){
		if (isset($this->{$object}) && is_object($this->{$object})){
			return $this->{$object};
		}
		return false;
	}
	
	public function head_render($head_render = null){
		if ($head_render === null) return $this->head_render;
		$this->head_render = (bool)$head_render;
		return $this;
	}
	public function foot_render($foot_render){
		if ($foot_render === null) return $this->foot_render;
		$this->foot_render = (bool)$foot_render;
		return $this;
	}
	public function main_render($main_render){
		if ($main_render === null) return $this->main_render;
		$this->main_render = (bool)$main_render;
		return $this;
	}
	public function content_render($content_render){
		if ($content_render === null) return $this->content_render;
		$this->content_render = (bool)$content_render;
		return $this;
	}
	public function content_left_render($content_left_render){
		if ($content_left_render === null) return $this->content_left_render;
		$this->content_left_render = (bool)$content_left_render;
		return $this;
	}
	public function content_right_render($content_right_render){
		if ($content_right_render === null) return $this->content_right_render;
		$this->content_right_render = (bool)$content_right_render;
		return $this;
	}
	public function extra_render($extra_render){
		if ($extra_render === null) return $this->extra_render;
		$this->extra_render = (bool)$extra_render;
		return $this;
	}
	
	public function style($style = null){
		if ($style === null)
			return $this->style;
		else
			$this->style = (string)$style;
		return $this;
	}
	
	public function script($script = null){
		if ($script === null)
			return $this->script;
		else
			$this->script = (string)$script;
		return $this;
	}
	
	public function endscript($script = null){
		if ($script === null)
			return $this->endscript;
		else
			$this->endscript = (string)$script;
		return $this;
	}
	
	public function js($js = null){
		if ($js === null)
			return $this->js;
		else
			$this->js = (array)$js;
		return $this;
	}
	
	public function add_js(array $js){
		$this->js = array_merge($this->js,$js);
		return $this;
	}
	
	public function endjs($js = null){
		if ($js === null)
			return $this->endjs;
		else
			$this->endjs = (array)$js;
		return $this;
	}
	
	public function add_endjs(array $js){
		$this->endjs = array_merge($this->endjs,$js);
		return $this;
	}
	
	public function css($css = null){
		if ($css === null)
			return $this->css;
		else
			$this->css = (array)$css;
		return $this;
	}
	
	public function add_css(array $css){
		$this->css = array_merge($this->css,$css);
		return $this;
	}
	
	public function ico($ico = null){
		if ($ico === null){
			$ico = DOCROOT.trim($this->ico,'/ ');
			if (is_file($ico) && is_readable($ico)){
				return '<link rel="shortcut icon" href="'.$this->ico.'" />'."\n";
			}
			return '';
		}else
			$this->ico = (string)$ico;
		return $this;
	}
	
	public function render(){
		View::bind_global('title',$this->title);
		View::bind_global('navigate',$this->navigate);
		View::bind_global('meta_keywords',$this->meta_keywords);
		View::bind_global('meta_description',$this->meta_description);
		View::set_global('ico',$this->ico());
		
		$template = View::factory($this->template);
		$template
		->set('style',is_file($this->style)?"\n".file_get_contents($this->style)."\n":'')
		->set('script',is_file($this->script)?"\n".file_get_contents($this->script)."\n":'')
		->set('endscript',is_file($this->endscript)?"\n".file_get_contents($this->endscript)."\n":'')
		->set('css',$this->css)
		->set('js',$this->js)
		->set('endjs',$this->endjs)
		->set('extra','')
		->set('head','')
		->set('main','')
		->set('foot','');
		
		if (true === $this->main_render){
			if (!isset($this->main)){
				$this->main = View::factory('template/main');
			}
			if ($this->main instanceof View){
				$this->main
				->set('content_left','')
				->set('content','')
				->set('content_right','');
				if (true === $this->content_left_render){
					$this->main->set('content_left',$this->content_left);
				}
				if (true === $this->content_render){
					$this->main->set('content',$this->content);
				}
				if (true === $this->content_right_render){
					$this->main->set('content_right',$this->content_right);
				}
			}
			$template->set('main',$this->main);
		}
		
		if (true === $this->head_render){
			$template->set('head',$this->head);
		}
		if (true === $this->foot_render){
			$template->set('foot',$this->foot);
		}
		if (true === $this->extra_render){
			$template->set('extra',$this->extra);
		}
		
		return $template->render();
	}
}